// 以下为官方示例
$().ready(function() {
	validateRule();
	$("#sex").val($("#sexHidden").val());
	$("#type").val($("#typeHidden").val());
	// $("#signupForm").validate();

	// let competitionType = sessionStorage.getItem('competitiontype');
	// $("#competitionType").val(competitionType);

	let track = sessionStorage.getItem('track');
	$("#track").val(track);

	if( $("#jobTypes").val() != '3' ){
		$("#job").remove();
	}

	$.ajax({
		cache : true,
		type : "GET",
		url : "/sys/user/competitionArea",
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				let str = '';
				data.data?.map(function (v,k){
					if( k == 0 ) str += `<option>请选择</option>`
					str += `<option value="${v.id}">${v.name}</option>`
				})
				$("#area").html(str);
				$("#area").val($("#areas").val());

				$("#area").change(function (ev){
					// console.log(ev.target.selectedIndex)
					$("#competitionAreaName").val($("#area option:selected").html());
					if( $("#area option:selected").html() == '请选择' ) 	$("#competitionAreaName").val('');
				})

			} else {
				layer.alert(data.msg)
			}

		}
	});


	let xmSele = [{name:'教师组',value:1},{name:'学生中职组',value:2},{name:'学生高校组',value:3}];
	let competitionType = $(".competitionType").val();
		competitionType = competitionType.split(';;');
	xmSelect.render({
		el: '#encipher',
		autoRow: true,
		name: '',
		max:3,
		initValue: competitionType,
		data: xmSele,
		maxMethod(seles, item) {
			layer.msg(`最多只能选择3个`)
		},
		on: function (data) {
			let arr = [];
			data.arr.map(function (v){
				arr.push(v.value);
			})
			$(".competitionType").val( arr.join(',').replace(/,/g,';;') );
		}
	})

});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$("#roleIds").val(getCheckedRoles());
	$.ajax({
		cache : true,
		type : "POST",
		url : "/sys/user/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg(data.msg);
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.msg(data.msg);
			}

		}
	});
}
function getCheckedRoles() {
	var adIds = "";
	$("input:checkbox[name=role]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}
function setCheckedRoles() {
	var roleIds = $("#roleIds").val();
	alert(roleIds);
	var adIds = "";
	$("input:checkbox[name=role]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			username : {
				required : true,
				minlength : 2
			},
			password : {
				required : true,
				minlength : 6
			},
			confirm_password : {
				required : true,
				minlength : 6,
				equalTo : "#password"
			},
			email : {
				// required : true,
				email : true
			},
			type : {
				required : true
			},
			topic : {
				required : "#newsletter:checked",
				minlength : 2
			},
			agree : "required"
		},
		messages : {

			name : {
				required : icon + "请输入姓名"
			},
			username : {
				required : icon + "请输入您的用户名",
				minlength : icon + "用户名必须两个字符以上"
			},
			password : {
				required : icon + "请输入您的密码",
				minlength : icon + "密码必须6个字符以上"
			},
			refereesNumber:{
				required : icon + "请输入裁判序号",
			},
			confirm_password : {
				required : icon + "请再次输入密码",
				minlength : icon + "密码必须6个字符以上",
				equalTo : icon + "两次输入的密码不一致"
			}
		}
	})
}
var openDept = function(){
	layer.open({
		type:2,
		title:"选择部门",
		area : [ '300px', '450px' ],
		content:"/sys/dept/treeView"
	})
}
function loadDept( deptId,deptName){
	$("#deptId").val(deptId);
	$("#deptName").val(deptName);
}

layui.use(['upload'], function() {
	var upload = layui.upload;
	let index;
	upload.render({
		elem: '#test1'
		,accept: ''
		,file:''
		,url: '/common/sysFile/upload'
		,choose: function(obj){
			console.log($("."+this.id).attr('class'))
			index = layer.load(1,{fixed:false,offset: 'auto'});
		}
		, done: function ( res ) {
			layer.close(index);
			$("#demo1").show();
			$("#demo1").find('img').attr('src', res.data );
			$("#img").val(res.data)
		}
		,error:function (res){
			layer.close(index);
			layer.msg('上传失败，请检查')
		}
	})
})